import { FlowRouter } from 'meteor/kadira:flow-router';
import { BlazeLayout } from 'meteor/kadira:blaze-layout';

// Import needed templates
import '../../ui/layouts/body/body.js';
import '../../ui/layouts/header/header.js';
import '../../ui/layouts/footer/footer.js';
import '../../ui/pages/home/home.js';
import '../../ui/pages/products/products.js';
import '../../ui/pages/product/product.js';
import '../../ui/pages/rule/rule.js';
import '../../ui/pages/contact/contact.js';
import '../../ui/pages/login/login.js';
import '../../ui/pages/reg/reg.js';
import '../../ui/pages/search/search.js';
import '../../ui/pages/about/about.js';

import '../../ui/pages/not-found/not-found.js';
import '../../ui/admin/layout/layout.js';
import '../../ui/admin/home/home.js';
import '../../ui/admin/roles/roles.js';
import '../../ui/admin/links/links.js';
import '../../ui/admin/users/users.js';

import '../../ui/admin/components/modals/new_link.js';
import '../../ui/admin/components/modals/new_role.js';
import '../../ui/admin/components/modals/edit_role.js';


import {pageScroll, toggleHide} from "./enter.js"

// Set up all routes in the app
var home = {
	home: "active"
}

FlowRouter.route('/', {
  name: 'App.home',
  action() {
    pageScroll();
    toggleHide();
    BlazeLayout.render('App_body', { header: 'App_Header', main: 'App_home', footer: "App_Footer"});

  },
});

FlowRouter.route('/products', {
  name: 'App.products',
  action() {
    pageScroll();
    toggleHide();
    BlazeLayout.render('App_body', { header: 'App_Header', main: 'Products_List', footer: "App_Footer"});
  },
});

FlowRouter.route('/products/:id', {
  name: 'App.products',
  action() {
    pageScroll();
    toggleHide();
    BlazeLayout.render('App_body', { header: 'App_Header', main: 'Product', footer: "App_Footer"});
  },
});

FlowRouter.route('/login', {
  name: 'App.login',
  action() {
    pageScroll();
    toggleHide();
    BlazeLayout.render('App_body', { header: 'App_Header', main: 'login', footer: "App_Footer"});
  },
});

FlowRouter.route('/reg', {
  name: 'App.products',
  action() {
    pageScroll();
    toggleHide();
    BlazeLayout.render('App_body', { header: 'App_Header', main: 'reg', footer: "App_Footer"});
  },
});

FlowRouter.route('/search', {
  name: 'App.products',
  action() {
    pageScroll();
    toggleHide();
    BlazeLayout.render('App_body', { header: 'App_Header', main: 'search', footer: "App_Footer"});
  },
});

FlowRouter.route('/rule', {
  name: 'App.products',
  action() {
    pageScroll();
    toggleHide();
    BlazeLayout.render('App_body', { header: 'App_Header', main: 'rule', footer: "App_Footer"});
  },
});

FlowRouter.route('/contact', {
  name: 'App.products',
  action() {
    pageScroll();
    toggleHide();
    BlazeLayout.render('App_body', { header: 'App_Header', main: 'contact', footer: "App_Footer"});
  },
});

FlowRouter.route('/about', {
  name: 'App.products',
  action() {
    pageScroll();
    toggleHide();
    BlazeLayout.render('App_body', { header: 'App_Header', main: 'about', footer: "App_Footer"});

  },
});


FlowRouter.route('/admin', {
  name: 'App.admin',
  action() {
		if (Meteor.userId() === null) {
			FlowRouter.go("/login");
		}
    pageScroll();
    toggleHide();

    Meteor.call("roles.permit", 500, function(error, result){
      if (!result) {
        FlowRouter.go("/login");
        Meteor.logout();
        FlashMessages.sendError("你当前没有权限进入系统，请与管理员联系xsqfeather@126.com");
      }
    });

    BlazeLayout.render('Admin_body', { main: 'Admin_home'});

  },
});





FlowRouter.route('/admin/roles/:page', {
  name: 'App.admin.roles',
	subscriptions: function(params, queryParams) {

        this.register('onepage.roles', Meteor.subscribe('roles.limit', parseInt(params.page)-1, 3));
    },
  action: function(params, queryParams) {
    if (Meteor.userId() === null) {
      FlowRouter.go("/login");
    }
    pageScroll();
    toggleHide();
    Meteor.subscribe('roles.limit', parseInt(params.page)-1, 3);
    BlazeLayout.render('Admin_body', { main: 'Admin_Roles', create: 'new_role', update: "edit_role"});
    FlashMessages.clear();

  },
});


FlowRouter.route('/admin/links/:page', {
  name: 'App.admin.links',
  subscriptions: function(params, queryParams) {

      this.register('onepage.links', Meteor.subscribe('links.limit', parseInt(params.page)-1, 3));

    },
  action: function(params, queryParams) {
    if (Meteor.userId() === null) {
      FlowRouter.go("/login");
    }
    pageScroll();
    toggleHide();
    BlazeLayout.render('Admin_body', { main: 'Admin_Links', create: 'new_link'});

  },
});

FlowRouter.route('/admin/users/:page', {
  name: 'App.admin.users',
  subscriptions: function(params, queryParams) {

      this.register('onepage.users', Meteor.subscribe('users.list', parseInt(params.page)-1, 3));

    },
  action: function(params, queryParams) {
    if (Meteor.userId() === null) {
      FlowRouter.go("/login");
    }
    pageScroll();
    toggleHide();
    BlazeLayout.render('Admin_body', { main: 'Admin_Users', create: 'new_user'});

  },
});

FlowRouter.route('/admin/tasks/:page', {
  name: 'App.admin.tasks',
  subscriptions: function(params, queryParams) {

      this.register('onepage.users', Meteor.subscribe('tasks.limit', parseInt(params.page)-1, 3));

    },
  action: function(params, queryParams) {
    if (Meteor.userId() === null) {
      FlowRouter.go("/login");
    }
    pageScroll();
    toggleHide();
    BlazeLayout.render('Admin_body', { main: 'Admin_Users', create: 'new_user'});

  },
});


FlowRouter.notFound = {
  name: 'App.404',
  action() {


    pageScroll();
    toggleHide();
    BlazeLayout.render('App_body', { main: 'App_notFound', header: 'App_Header', footer: "App_Footer" });
  },
};
